import { ApiarticleList } from "../../../../api/index"
const app = getApp()
Component({
  lifetimes: {
    attached() {
      this.init(this.data.words)
    }
  },
  properties: {
    words: {
      type: String,
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    parmas: {
      page: 1,
      size: 10,
      finish: false
    },
    list: [],
    loading: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init(words) {
      this.setData({
        list: [],
      },() => {
        this.data.parmas.page = 1
        this.data.parmas.words = words
        this.getList()
      })
    },

    onLoadMore() {
      if(this.data.loading || this.data.parmas.finish) return
      this.data.parmas.page += 1
      this.getList()
    },

    getList() {
      return new Promise(resolve => {
        this.setData({
          loading: true
        })
        ApiarticleList(this.data.parmas).then(res => {
          const data = res.data.map(item => {
            item.content = item.content.replace(/(<\/?.+?\/?>)|&nbsp;|\s/g, '').slice(0,100)
            item.pic = app.setPath(item.pic)
            item.created_at = item.created_at.replace(/^(\d{4})-(\d{2})-(\d{2}).*/,"$1年$2月$3日")
            return item
          })
          const list = this.data.parmas.page === 1 ? data : this.data.list.concat(data)
          this.data.parmas.finish = list.length >= res.page.total
          this.setData({
            list,
          })
        }).finally(_=>{
          this.setData({
            loading: false
          })
          resolve()
        })
      })
      
    }
  }
})